package com.qingjiang.projectschedule.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @description:
 * @author: yyg
 * @date: 2024/3/20
 */
public class DateUtil {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 以当前时间为准，checkTime超期了多少天
     *
     * @param checkTime 待比较的时间
     */
    public static long calDayDiff(Date checkTime) {
        Instant instant = checkTime.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        LocalDate today = LocalDate.now();

        long diff = localDate.until(today, ChronoUnit.DAYS);
        return diff;
    }

    /**
     * 记录时间-结束时间，记录时还有剩余时间则返回正数
     *
     * @param recordTime 记录时间
     * @param finishTime 结束时间
     * @return
     */
    public static long calDayDiff(Date recordTime, Date finishTime) {
        Instant instant1 = recordTime.toInstant();
        Instant instant2 = finishTime.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDate date1 = instant1.atZone(zoneId).toLocalDate();
        LocalDate date2 = instant2.atZone(zoneId).toLocalDate();
        long diff = date1.until(date2, ChronoUnit.DAYS);
        return diff;
    }

    public static Date localDate2Date(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static Date string2Date(String s) {
        Date date;
        try {
            date = dateFormat.parse(s);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    public static String Date2String(Date date) {
        return dateFormat.format(date);
    }

    public static Date DateAdd(Date date, int days) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, days);
        date = calendar.getTime();
        return date;
    }

    public static String DateConvert(Date time) {
        if (time == null) {
            return null;
        }
        if (isThisYear(time)) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            String strDate = sdf.format(time);
            return strDate;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = sdf.format(time);
        return strDate;
    }

    public static boolean isThisYear(Date time) {
        return isThisTime(time, "yyyy");
    }

    private static boolean isThisTime(Date time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(time);
        String now = sdf.format(new Date());
        if (param.equals(now)) {
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        Date date = string2Date("2023-03-21");

        System.out.println(DateConvert(date));
        System.out.println(date);
        LocalDate today = LocalDate.now();
        System.out.println(localDate2Date(today));

        Date record = string2Date("2024-03-25");
        Date finish = string2Date("2024-03-30");
        System.out.println(DateUtil.calDayDiff(record, finish));


        Date start = string2Date("2024-03-25");
        Date now = new Date();
        System.out.println(DateUtil.calDayDiff(start, now));
        System.out.println(DateUtil.calDayDiff(start));
    }
}
